package com.atguigu.flink.chapter06;

import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.windowing.ProcessWindowFunction;
import org.apache.flink.streaming.api.windowing.assigners.GlobalWindows;
import org.apache.flink.streaming.api.windowing.triggers.Trigger;
import org.apache.flink.streaming.api.windowing.triggers.TriggerResult;
import org.apache.flink.streaming.api.windowing.windows.GlobalWindow;
import org.apache.flink.util.Collector;

import java.util.ArrayList;

/*
自定义触发器
 */
public class WindowGlobal1 {
    public static void main(String[] args) {
        Configuration conf = new Configuration();
        conf.setInteger("rest.port", 2000);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
        env.setParallelism(1);

        env.socketTextStream("hadoop102",8888)
                .flatMap(new FlatMapFunction<String, Tuple2<String,Integer>>() {
                    @Override
                    public void flatMap(String s,
                                        Collector<Tuple2<String, Integer>> out) throws Exception {
                        String[] words = s.split(" ");
                        for (String word : words) {
                            out.collect(Tuple2.of(word,1));
                        }
                    }
                })
                .keyBy(t -> t.f0)
                .window(GlobalWindows.create())
                .trigger(new Trigger<Tuple2<String, Integer>, GlobalWindow>() {
                    //每来一个元素触发一次
                    int count = 0;
                    @Override
                    public TriggerResult onElement(Tuple2<String, Integer> elements,
                                                   long timestamp,
                                                   GlobalWindow window,
                                                   TriggerContext ctx) throws Exception {
                        count++;
                        if (count == 5){
                            count = 0;
                            return TriggerResult.FIRE_AND_PURGE;
                        }else {
                            return TriggerResult.CONTINUE;
                        }
                    }

                    //当窗口基于处理时间触发
                    @Override
                    public TriggerResult onProcessingTime(long time,
                                                          GlobalWindow window,
                                                          TriggerContext ctx) throws Exception {
                        return TriggerResult.CONTINUE;
                    }

                    @Override
                    public TriggerResult onEventTime(long l, GlobalWindow globalWindow, TriggerContext triggerContext) throws Exception {
                        return TriggerResult.CONTINUE;
                    }

                    //如何对窗口内的元素进行清空
                    @Override
                    public void clear(GlobalWindow globalWindow, TriggerContext triggerContext) throws Exception {

                    }
                })
                .process(new ProcessWindowFunction<Tuple2<String, Integer>, String, String, GlobalWindow>() {
                    @Override
                    public void process(String key,
                                        Context ctx,
                                        Iterable<Tuple2<String, Integer>> elements,
                                        Collector<String> out) throws Exception {
                        ArrayList<String> list = new ArrayList<>();
                        for (Tuple2<String, Integer> t : elements) {
                            list.add(t.f0 + "_" + t.f1);
                        }
                        String msg = list + "";
                        out.collect(msg);
                    }
                })
                .print();

        try {
            env.execute();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
